{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.\n",
    "\n",
    "# Random agent playing the Capture The Flag toy environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import logging\n",
    "import gym\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=\"%(levelname)s: %(message)s\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CyberBattle simulation\n",
    "- **Environment**: a network of nodes with assigned vulnerabilities/functionalities, value, and firewall configuration\n",
    "- **Action space**: local attack | remote attack | authenticated connection\n",
    "- **Observation**: effects of action on environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import cyberbattle._env.cyberbattle_env\n",
    "gym_env = gym.make('CyberBattleToyCtf-v0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gym_env.environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gym_env.action_space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gym_env.action_space.sample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": false,
    "tags": [
     "outputPrepend"
    ]
   },
   "source": [
    "## A random agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false,
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "for i_episode in range(1):\n",
    "    observation = gym_env.reset()\n",
    "\n",
    "    total_reward = 0\n",
    "\n",
    "    for t in range(5600):\n",
    "        action = gym_env.sample_valid_action()\n",
    "\n",
    "        observation, reward, done, info = gym_env.step(action)\n",
    "        \n",
    "        total_reward += reward\n",
    "        \n",
    "        if reward>0:\n",
    "            print('####### rewarded action: {action}')\n",
    "            print(f'total_reward={total_reward} reward={reward}')\n",
    "            gym_env.render()\n",
    "    \n",
    "        if done:\n",
    "            print(\"Episode finished after {} timesteps\".format(t+1))\n",
    "            break\n",
    "\n",
    "    gym_env.render()\n",
    "\n",
    "gym_env.close()\n",
    "print(\"simulation ended\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### End of simulation"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('base': conda)",
   "name": "python385jvsc74a57bd09f0194299e911b6a22f0cd3d1c9a66c991d39f48b249be23f24104e40900e329"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "metadata": {
   "interpreter": {
    "hash": "9f0194299e911b6a22f0cd3d1c9a66c991d39f48b249be23f24104e40900e329"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}